﻿using System.Collections.Generic;
using System.Threading.Tasks;
using cmdb.keede.common;
using cmdb.keede.data;
using cmdb.keede.entity;
using cmdb.keede.service.Contract;

namespace cmdb.keede.service
{
    public class GroupService : IGroupService
    {
        readonly IGroupRepository _groupRepository;
        readonly IUnitOfWork _unitOfWork;

        public GroupService(IUnitOfWork unitOfWork, IGroupRepository groupRepository)
        {
            _unitOfWork = unitOfWork;
            _groupRepository = groupRepository;
        }

        public async Task<BizResult<List<Group>>> GetGroupesAsync(int pageIndex, int pageSize)
        {
            return await _groupRepository.GetPageAsync(new Pager(pageIndex, pageSize),
                w => w.Status == EnumGroupStatus.Enabled, o => o.Name, false);
        }

        public async Task<BizResult<Group>> GetGroupAsync(int id)
        {
            var obj = await _groupRepository.GetByIdAsync(id);
            return new BizResult<Group>(obj);
        }

        public async Task<BizResult<bool>> DeleteGroupAsync(int id)
        {
            var obj = await _groupRepository.GetByIdAsync(id);
            obj.Status = EnumGroupStatus.Disabled;
            await _unitOfWork.CommitAsync();
            return new BizResult<bool>(true);
        }

        public async Task<BizResult<bool>> AddOrUpdateAppInfoAsync(Group group)
        {
            if (group.Id==0)
            {
                group.Status=EnumGroupStatus.Enabled;
                _groupRepository.Add(group);
            }
            else
            {
                var obj = await _groupRepository.GetByIdAsync(group.Id);
                obj.Description = group.Description;
                obj.Name = group.Name;
                obj.Status = group.Status;
                obj.CreatorName = group.CreatorName;
            }

            await _unitOfWork.CommitAsync();
            return new BizResult<bool>(true);
        }

        public async Task<BizResult<bool>> AddOrUpdateAppInfoAsync(GroupRequest request)
        {



            return new BizResult<bool>(true);
        }
    }
}
